import 'package:qm_line/constant.dart';

import 'qm_line_platform_interface.dart';
import 'model/response.dart';

typedef Future<dynamic> EventHandler(QMResponse response);
typedef Future<dynamic> MessageHandler(dynamic event);

class QmLine {
  // setMethodCallHandler(Future<dynamic> Function(MethodCall)? handler) {
  //   return QmLinePlatform.instance.setMethodCallHandler(handler);
  // }

  addEventHandler({
    EventHandler? onInitScuuess,
    EventHandler? onInitFailed,
    MessageHandler? newMessage,
    MessageHandler? customOnline,
    MessageHandler? customOffline,
    MessageHandler? customClaim,
    MessageHandler? customFinish,
    MessageHandler? customQueue,
    MessageHandler? customAgentInfo,
    MessageHandler? customInvestigate,
    MessageHandler? customVIP,
    MessageHandler? changeCustomStatus,
    MessageHandler? withdrawMessageStatus,
    // required MessageHandler robotAction,
    // required MessageHandler customLeavemsg,
    // required MessageHandler customSatisfaction,
    // required MessageHandler customAssociatsInput,
    // required MessageHandler refreshVoiceMessage,
  }) {
    return QmLinePlatform.instance.addEventHandler((call) => Future<dynamic>(() {
          try {
            switch (call.method) {
              case MethodKey.initScuuess:
                if (onInitScuuess != null) {
                  onInitScuuess(QMResponse.fromMap(call.arguments));
                }
                break;
              case MethodKey.initFailed:
                if (onInitFailed != null) {
                  onInitFailed(QMResponse.fromMap(call.arguments));
                }
                break;
              case MethodKey.newMessage:
                if (newMessage != null) {
                  newMessage(call.arguments);
                }
                break;
              case MethodKey.customOnline:
                if (customOnline != null) {
                  customOnline(null);
                }
                break;
              case MethodKey.customOffline:
                if (customOffline != null) {
                  customOffline(null);
                }
                break;
              case MethodKey.customClaim:
                if (customClaim != null) {
                  customClaim(null);
                }
                break;
              case MethodKey.customFinish:
                if (customFinish != null) {
                  customFinish(null);
                }
                break;
              case MethodKey.customQueue:
                if (customQueue != null) {
                  customQueue(call.arguments);
                }
                break;
              case MethodKey.customAgentInfo:
                if (customAgentInfo != null) {
                  customAgentInfo(call.arguments);
                }
                break;
              case MethodKey.customInvestigate:
                if (customInvestigate != null) {
                  customInvestigate(call.arguments);
                }
                break;
              case MethodKey.customVIP:
                if (customVIP != null) {
                  customVIP(null);
                }
                break;
              case MethodKey.changeCustomStatus:
                if (changeCustomStatus != null) {
                  changeCustomStatus(null);
                }
                break;
              case MethodKey.withdrawMessageStatus:
                if (withdrawMessageStatus != null) {
                  withdrawMessageStatus(call.arguments);
                }
                break;
              case MethodKey.robotAction:
              case MethodKey.customLeavemsg:
              case MethodKey.customSatisfaction:
              case MethodKey.customAssociatsInput:
              case MethodKey.refreshVoiceMessage:
                break;
              default:
                throw UnsupportedError("Unrecognized Event");
            }
          } catch (e) {
            throw UnsupportedError(e.toString());
          }
        }).catchError((e) {
          throw UnsupportedError(e.toString());
        }));
  }

  /// 配置服务环境
  /// serviceLine - aliy：阿里云  tencent：腾讯云
  void switchServiceRoute(QMServiceLine serviceLine) {
    return QmLinePlatform.instance.switchServiceRoute(serviceLine);
  }

  /// 初始化SDK（初始化前需要先配置服务环境）
  /// accessId：接入客服系统的密钥， 登录web客服系统（渠道设置->移动APP客服里获取）
  /// userName：用户名， 区分用户， 用户名可直接在后台会话列表显示
  /// userId：用户ID， 区分用户（只能使用  数字 字母(包括大小写) 下划线 短横线）
  Future<QMResponse> initSDK(String accessId, String userName, String userId) {
    return QmLinePlatform.instance.initSDK(accessId, userName, userId);
  }

  /// 关闭服务，断开连接
  /// 客服人员可收到用户离开的通知
  void quitSDK() {
    return QmLinePlatform.instance.quitSDK();
  }

  /// 开始会话
  /// 进入聊天界面, 首先调用开始会话接口, 成功后才能获取坐席状态, 及消息的收发, 参数为刚刚选择的部门ID, 回调是否开启评价选择项
  /// param：扩展信息
  Future<QMResponse> beginChatSession(String peerId, Map<dynamic, dynamic> param) {
    return QmLinePlatform.instance.beginChatSession(peerId, param);
  }

  /// 发送文本消息
  /// 可以发送文字、表情
  Future<QMResponse> sendTextMessage(String text) {
    return QmLinePlatform.instance.sendTextMessage(text);
  }

  /// 发送图片消息
  Future<QMResponse> sendImageMessage(String imagePath) {
    return QmLinePlatform.instance.sendImageMessage(imagePath);
  }

  /// 发送文件消息
  /// filePath：文件路径（iOS为文件名）
  /// fileName：文件名
  /// fileSize：文件大小
  Future<QMResponse> sendFileMessage(String filePath, String fileName, String fileSize) async {
    return QmLinePlatform.instance.sendFileMessage(filePath, fileName, fileSize);
  }

  /// 消息重发
  Future<QMResponse> resendMessage(String messageId) {
    return QmLinePlatform.instance.resendMessage(messageId);
  }

  /// 留言
  /// peer：技能组
  Future<QMResponse> leaveMessage(String peerId, String message) {
    return QmLinePlatform.instance.leaveMessage(peerId, message);
  }

  /// 获取消息列表
  /// page：第几页
  Future<QMResponse> getMessageList(int page) {
    return QmLinePlatform.instance.getMessageList(page);
  }

  /// 获取单条数据库信息
  /// 调用此接口、获取数据库一条记录、用于消息重发
  Future<QMResponse> getMessage(String messageId) async {
    return QmLinePlatform.instance.getMessage(messageId);
  }

  /// 删除单条数据库信息
  /// 用此接口、删除数据路一条记录、只能删除本地消息
  Future<QMResponse> deleteMessage(String messageId) async {
    return QmLinePlatform.instance.deleteMessage(messageId);
  }

  /// 获取未读消息数
  Future<QMResponse> getUnreadMessageCount(String accessId, String username, String userId) {
    return QmLinePlatform.instance.getUnreadMessageCount(accessId, username, userId);
  }

  /// 获取未读消息列表
  /// 通过外部标记的截止已读消息ID，来获取未读消息列表
  /// afterMsgId：指定msg的ID
  Future<QMResponse> getUnreadMessageList(String afterMsgId) {
    return QmLinePlatform.instance.getUnreadMessageList(afterMsgId);
  }
}
